/*轮播图*/
var swiper = new Swiper('.swiper-container', {
	pagination: '.swiper-pagination',
	paginationClickable: true,
	spaceBetween: 30,
	centeredSlides: true,
	autoplay: 2500,
	autoplayDisableOnInteraction: false,
});

/*vue实例*/
var myvue = new Vue({
	el: "body",
	data: {
		hasadress: true,
		myadress: "",
		choiceadress: "",
		ownPoint: "",
		shop: [],
		shopstar: [],
		shopattr1: [],
		shopattr2: [],
		b2cFar: [],
		mhss: ""
	},
	created: function() {
		this.leancloudInit();
		this.baiduInit();
	},
	watch: {
		ownPoint: function() {
			this.howFar();
		}
	},
	methods: {
		leancloudInit: function() {
			var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
			var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
			AV.init({
				appId: APP_ID,
				appKey: APP_KEY
			});
		},
		baiduInit: function() {
			var _this = this;
			/*117.103144,39.083597海泰的坐标*/
			/*默认定位到天津工业大学*/
			//			var zbX = 117.118987;
			//			var zbY = 39.071304;
			var map = new BMap.Map("allmap"); // 创建Map实例
			//			var nowPoint = new BMap.Point(zbX, zbY);
			//			_this.ownPoint = nowPoint;
			/*定位到当前位置*/
			var geolocation = new BMap.Geolocation();
			geolocation.getCurrentPosition(function(r) {
				if(this.getStatus() == BMAP_STATUS_SUCCESS) {
					var mk = new BMap.Marker(r.point);
					map.addOverlay(mk);
					map.panTo(r.point);
					/*现在定位的坐标*/
					zbX = r.point.lng;
					zbY = r.point.lat;
					/*将当前坐标转化为中文显示在头部div中*/
					var nowPoint = new BMap.Point(zbX, zbY);
					//将当前坐标存入vue
					_this.ownPoint = nowPoint;
					var geoc = new BMap.Geocoder();
					geoc.getLocation(nowPoint, function(rs) {
						var addComp = rs.addressComponents;
						/*定位的中文地址*/
						_this.myadress = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
						_this.howFar();
					});
				} else {
					alert('定位失败，请重新选择地址');
				}
			}, {
				enableHighAccuracy: true
			})
		},
		setAdress: function() {
			var _this = this;
			// 创建地址解析器实例
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			//input输入后将中文解析为坐标
			myGeo.getPoint(_this.choiceadress, function(point) {
				if(point) {
					console.log(_this.choiceadress);
					//将坐标和位置存入vue中，并改变首页的中文地址
					_this.ownPoint = point;
					_this.myadress = _this.choiceadress;
				} else {
					alert("请您输入正确格式(省+市+区+具体位置)");
				}
			});
		},
		howFar: function() {
			this.shop = [];
			this.shopstar = [];
			this.shopattr1 = [];
			this.shopattr2 = [];
			this.b2cFar = [];
			//根据距离筛选商家
			var _this = myvue;
			var juli;
			// 创建地址解析器实例
			var map = new BMap.Map("allmap");
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			var query = new AV.Query('ELM_shop');
			query.find().then(function(data) {
				for(var i = 0; i < data.length; i++) {
					//使用闭包将i存入
					(function(i) {
						myGeo.getPoint(data[i].attributes.adress, function(point) {
							if(point) {
								/*商家和我现在的距离*/
								juli = map.getDistance(_this.ownPoint, point).toFixed(2);
								if(juli < 50000) {
									_this.shop.push(data[i]);
									_this.shopstar.push(parseInt(data[i].attributes.grade));
									_this.shopattr1.push(eval(data[i].attributes.attr1));
									_this.shopattr2.push(eval(data[i].attributes.attr2));
									_this.b2cFar.push((juli / 1000).toFixed(2));
								}
							} else {
								alert("请您输入正确格式(省+市+区+具体位置)");
							}
						})
					})(i);
				}
			}, function(error) {
				// 异常处理
			});

		},
		showadress: function() {
			this.hasadress = !(this.hasadress);
		},
		backlist: function() {
			this.hasadress = !(this.hasadress);
		},
		tz: function(classify) {
			location.href = "../shoplist/shoplist.html?classify=" + seturl(classify) + "&zbx=" + this.ownPoint.lng + "&zby=" + this.ownPoint.lat;
		},
		tzshopdata: function(id) {
			location.href = "../shopdata/shopdata.html?shopid=" + seturl(id);
		},
		mhget: function() {
			var mhss=this.mhss;
			location.href = "../shoplist/shoplist.html?classify=" + seturl(mhss) + "&zbx=" + this.ownPoint.lng + "&zby=" + this.ownPoint.lat;
		}

	}
})